package com.hc.service;


import com.hc.domain.Good;
import com.hc.domain.Order;
import com.hc.domain.OrderStatus;
import com.hc.feign.AccountFeignClient;
import com.hc.feign.GoodFeignClient;
import com.hc.feign.OrderFeignClient;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BusinessService {


  /*  @Autowired
    RestTemplate restTemplate;*/

    @Autowired
    OrderFeignClient orderFeignClient;

    @Autowired
    GoodFeignClient goodFeignClient;

    @Autowired
    AccountFeignClient accountFeignClient;


    @GlobalTransactional
    public void placeOrder(int accountId, int goodId, int num) {
        Good good = goodFeignClient.findById(goodId);
        double amount = good.getGoodPrice() * num;
        Order order = new Order().setGoodId(goodId).setGoodNum(num).setAccountId(accountId).setOrderAmount(amount).setStatus(OrderStatus.CREATE.getValue());
        // 创建订单
        orderFeignClient.addOrder(order);
        // 减库存
        goodFeignClient.reduceStock(goodId, num);
        // 减余额
        accountFeignClient.reduceMoney(accountId, amount);
    }
}
